import FormLyout from '@/components/commonPublic/fromLayout.vue'
import formJs from "./regexDetail.js"; //新增弹框配置项
import actions from "@/actions";
import publicForm from "@/components/public/el-form.vue"; //弹框form
import messageBox from "@/components/public/messageBox"; //弹框
import TableData from "@/components/waterManage/public/el-tables"; //主页面列表
import {
    getPierTaskPageList,
    getPierTaskByFid,
    addPierTask,
    deletePierTaskByFid,
    submitPierTask,
    upload, //上传文件
    getPierProjectListByCompanyId,
    getBaseDataitemDetailByItemCode,
    getUserCompanyTreeList,
    getBaseCompanyList,
    getBaseDepartmentUser,
} from "@/api/request.js";
export default {
    name: "monitoringDetail",
    data() {
        return {
            name: "monitoringDetail",
            formObj: formJs.formObj,
            formData: {
                // 任务表信息
                fId: "", // 检测任务信息表fid  有值时为修改  其余为新增
                fCompanyid: "", // 施工标段（单位）id  当前登录人
                fCompanyname: "", // 施工标段（单位）名称  当前登录人
                fProjectid: "", // 工程id   当前登录人
                fProjectname: "", // 工程名称  当前登录人
                fCheckcontent: "", // 检测项目
                fCheckfoundationcount: "", // 本次检测基桩数量
                fInspectiondates: "", // 报验日期
                fContactsandtel: "", //联系人和电话
                fCheckdates: "", // 检测日期
                memberList: [],
                fCheckerids: "", // 检测人员ids（字符串拼接","隔开）
                fCheckernames: "", // 检测人员names（字符串拼接","隔开）
                fIslastcheck: "", // 是否最后一次检测 0-否  1-是
                fCheckfile: "", // 本次检测附件资料id
                // 子表信息
                pierfoundationList: [],
                // 当前用户信息
                userId: "", // 当前用户id
                userName: "", // 当前用户名称
                recordfiles: [],
            },

            msgTip: "请先选中数据行！",
            msgType: 1,
            showMsgBox: false,
            projectsObj: [], //项目集合
            activeIndex: "1",
            isSave: false,
            lrCheckPierfoundation: [], // 基桩检测表格信息集合
            samllTable: [],
            table_config: formJs.table_config,
            lrCheckPiertask: formJs.lrCheckPiertask,
            lrCheckPiertaskData: {},



            fPhotofilelist: [],
            dis: false,
            timestamp1: null,
            dialogPdf: false,
            pdfUrl: "",
            fFileName: "",
            companyTree: [],
            projectList: [],
            companyList: [],
            // 列表
            getList: {
                modelType: "0", // 0代办 1已办
                fUserid: null, //当前登录用户id
                page: 1, //页码
                pageSize: 20, //每页条数
            },
            navigatepageNums: null, //是否显示分页
            queryCriteriaData: {},
            flagNum: 0,
            // 上传附件
            disabled: false,
            dialogTableVisibleTexts: false,
            loadingDialogTexts: false,
            enclosureImg: {},
            timestamp: null,
            // attachmentDetails: [],
            imgList: [],
            recordfilesList: [],


            radio: "待办",
            dialogHeight: "",
            dialogHeights: "",
            fId: null, //当前数据fId
            loading: false,
            userInfo: {}, //登录用户


            // 查看
            loadingDialog: false,


            row: null,
            formLabelWidth: "80px",
            tableData: [], //表格数据列表

        };
    },
    created() {

    },
    components: {
        FormLyout,
        publicForm,
        messageBox,
        TableData
    },
    mounted() {


        actions.onGlobalStateChange((state, prev) => {
            this.userInfo = prev.currentUser
            this.getList.fUserid = this.userInfo.id;
            var data = prev.currentUser.projects
            this.projectsObj = [{
                fItemName: "所有项目",
                fItemValue: "",
            }, ];
            data.map((item) => {
                this.projectsObj.push({
                    fItemName: item.projectName,
                    fItemValue: item.projectId,
                });
            });


        }, true);

        this.yj_edit()
    },
    methods: {
        handleSelect(key) {
            this.activeIndex = key;
        },

        confirmMsg() {
            this.showMsgBox = false;
        },
        yj_saveForm() {
            if (this.formData.fIslastcheck == "否") {
                this.formData.fIslastcheck = "0";
            } else if (this.formData.fIslastcheck == "是") {
                this.formData.fIslastcheck = "1";
            }
            // this.formData.fRegistrantid = this.userInfo.id;
            // this.formData.fRegistrantname = this.userInfo.userName;
            this.formData.userId = this.userInfo.id;
            this.formData.userName = this.userInfo.userName;
            // this.formData.fDesignfile = "EQUIP" + Date.parse(new Date());;
            if (this.$refs.publicForm.submitForm()) {
                addPierTask(this.formData)
                    .then((res) => {
                        if (res.errcode == 0) {
                            // this.$router.push("/pcManage/engineeringDetail")
                            // this.$router.push({
                            //     path: "/pcManage/engineeringDetail",

                            // });
                            this.$router.push("/pcManage/monitoringDetail");
                        } else {
                            this.showMsgBox = true;
                            this.msgTip = "保存失败"
                            // this.$message.error("保存失败", res.msg);

                        }
                    })
                    .catch(() => {
                        console.log("保存失败");

                    });
            } else {
                console.log("error");
            }
            console.log(55, this.formData);
        },

        delteFile(file) { //删除文件
            for (let index = 0; index < this.fPhotofilelist.length; index++) {
                if (file.uid == this.fPhotofilelist[index].uid) {
                    if (this.fPhotofilelist[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fPhotofilelist[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fPhotofilelist[index].atta.fId,
                        });
                    }
                    this.fPhotofilelist.splice(index, 1);
                }
            }
        },



        // 查看
        editText(num) {
            var fId = JSON.parse(this.$route.query.data).fId

            getPierTaskByFid({
                    fId: fId
                })
                .then((res) => {
                    console.log(5656, res);
                    if (res.errcode == 0) {
                        var that = this;
                        that.loadingDialogText = false;
                        that.dialogTableVisibleText = true;
                        that.formData = res.data.lrCheckPiertask;
                        if (that.formData.fIslastcheck == "0") {
                            that.formData.fIslastcheck = "否";
                        } else {
                            that.formData.fIslastcheck = "是";
                        }
                        that.timestamp = res.data.lrCheckPiertask.fCheckfile;
                        that.fCheckfileList = res.data.checkFile;
                        that.formData.recordfiles = [];
                    } else {
                        this.$message.error("编辑", res.msg);

                    }
                })
                .catch(() => {
                    console.log("编辑接口出错");

                });

        },
        // 获取当前日期
        getDate() {
            var time = new Date();
            const y = time.getFullYear();

            var m = time.getMonth() + 1 + "";
            m = (m < 10 ? "0" : "") + m;

            var d = time.getDate() + "";
            d = (d < 10 ? "0" : "") + d;

            this.formData.fInspectiondates = `${y}-${m}-${d}`;
            this.formData.fCheckdates = `${y}-${m}-${d}`;
            this.pierfoundationList.fPourdates = `${y}-${m}-${d}`;
        },
        // 新增/编辑
        yj_edit(num) {

            var num = this.$route.query.status
            // this.formData = {
            //     fId: "", // 工程信息fid  有值时表示修改  其余表示新增
            //     fCompanyid: "", // 施工单位id
            //     fCompanyname: "", // 施工单位名称
            //     fProjectid: "", // 项目id
            //     fProjectname: "", // 项目名称
            //     fEngineeringname: "", // 工程名称
            //     fEngineeringlocation: "", // 工程地点
            //     fEngineeringinfo: "", // 工程信息简述
            //     fDesignfile: "", // 设计资料文件id
            //     fPiercount: "", // 墩台数量
            //     fFoundationcount: "", // 基桩数量
            //     fRegistrantid: "", // 登记人id
            //     fRegistrantname: "", // 登记人名称
            //     fRegistdates: "", // 登记时间
            //     // 当前用户数据
            //     userId: "", // 当前登陆用户id
            //     userName: "", // 当前登陆用户名称
            //     recordfiles: [], //删除文件id集合--修改时传
            // };
            this.formData = this.$options.data.call(this).formData
            this.formData.fProjectname = this.projectsObj[1].fItemName
            this.formData.fProjectid = this.projectsObj[1].fItemValue;
            // this.flagNum = 0;
            // this.dis = false;

            if (num == 0) {
                this.isSave = true
                // 新增
                if (this.getList.fUserid != "System") {
                    this.getDate();
                    // 附件资料
                    this.attachmentDetails = [];
                    this.fCheckfileList = [];
                    this.timestamp = "ZLFJ" + Date.parse(new Date()); //资料附件
                    this.formData.fCheckfile = this.timestamp;
                    this.formObj.map(item => {
                        item.disabled = false
                    })
                    this.formObj[6].disabled = true
                    this.fPhotofilelist = [];
                } else {
                    console.log("该用户无法新增");
                    // this.$message.warning("该用户无法新增");
                }
            } else if (num == 1) {
                this.isSave = true

                // 编辑
                this.editText(num);
                this.formObj.map(item => {
                    item.disabled = false
                })
                this.formObj[6].disabled = true
                //     formObj
                // disabled
            } else if (num == 4) {
                this.isSave = false

                // 查看
                this.editText(num);
                // this.getPierProjectTaskLists()
                this.formObj.map(item => {
                    item.disabled = true
                })
            }
        },
        // 弹框内+/-按钮
        newList(num) {
            this.pierfoundationList = {
                fid: "", //唯一标识
                fPiernumber: "", // 桩台编号
                fPourdates: "", // 灌注日期
                fMethod: "", // 成孔方法
                fStrengthgrade: "", // 强度等级
                fPiertype: "", // 桩型
                fCheckmethod: "", // 检测方法
                fPiercategory: "", // 桩类别
                fScenepicfile: "", // 现场照片id
                fCheckfile: "", // 检测资料id
                fIsexception: "无", // 是否异常
                fExceptioninfo: "", // 异常说明
                fHandlerpicfile: "", // 处理照片id
                fExceptionfile: "", // 异常资料id
                picsfiles: [
                    // 现场照片
                    // {
                    //     "fId": "wu_1dis77ggt1nqk96e14p6pvd1tgma", // 文件fid
                    //     "filePath": "/fileAnnexes/9c14p6pvd1tgma.jpg", // 文件地址
                    //     "fFileName": "资料证书.jpg", // 文件名称
                    //     "fFileType": "jpg" // 文件格式
                    // }
                ],
                handlerfiles: [
                    // 处理照片
                    // {}
                ],
                checkfiles: [
                    // 检测资料
                    // {}
                ],
                exceptionfiles: [
                    // 异常资料
                    // {}
                ],
            };
            if (num == "0") {
                // 新增
                this.getDate();
                this.textTitle = "新增基桩检测";
                this.dialogTableVisible = true;
                // 现场照片
                this.attachmentDetails1 = [];
                this.fScenepicfileList = [];
                this.timestamp1 = "XCZP" + Date.parse(new Date());
                this.pierfoundationList.fScenepicfile = this.timestamp1;
                // 现场照片

                // 检测资料
                this.attachmentDetails2 = [];
                this.fCheckfilesList = [];
                this.timestamp2 = "JCZL" + Date.parse(new Date());
                this.pierfoundationList.fCheckfile = this.timestamp2;
                // 检测资料

                // 处理照片
                this.attachmentDetails3 = [];
                this.fHandlerpicfileList = [];
                this.timestamp3 = "CLZP" + Date.parse(new Date());
                this.pierfoundationList.fHandlerpicfile = this.timestamp3;
                // 处理照片

                // 异常资料
                this.attachmentDetails4 = [];
                this.fExceptionfileList = [];
                this.timestamp4 = "YCZL" + Date.parse(new Date());
                this.pierfoundationList.fExceptionfile = this.timestamp4;
                // 异常资料

                this.pierfoundationList.fid = "ZJ" + Date.parse(new Date()); //唯一标识
                if (this.pierfoundationList.fIsexception == "无") {
                    this.details = true;
                }
            } else {
                // 删除
                if (this.formData.pierfoundationList.length > 0) {
                    if (this.fPiernumber != "") {
                        for (
                            let index = 0; index < this.formData.pierfoundationList.length; index++
                        ) {
                            const element = this.formData.pierfoundationList[index];
                            if (this.fPiernumber == element.fId) {
                                this.formData.pierfoundationList.splice(index, 1);
                            }
                        }
                        this.formData.fCheckfoundationcount =
                            this.formData.pierfoundationList.length.toString();
                    } else {
                        return this.$message.error("请先选择需要刪除的数据");
                    }
                } else {
                    return this.$message.error("当前无可删除数据");
                }
            }
        },


        // 检测任务信息列表
        getPierProjectTaskLists() {
            this.samllTable = [];
            // this.lrCheckPiertask = {};
            // this.checkFile = [];
            // this.lrCheckPierfoundation = []; // 基桩检测表格信息集合
            getPierProjectTaskLists({
                    fId: JSON.parse(this.$route.query.data).fId
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.samllTable = res.data;
                        // this.equipmentDetailIds = res.data[0].fId;
                        this.getPierTaskByFid(res.data[0].fId);
                    } else {
                        return this.$message.error("检测任务信息列表 获取失败", res.msg);
                    }
                })
                .catch(() => {
                    console.log("检测任务信息列表 获取失败");
                    return false;
                });
        },
        getPierTaskByFid(id) {
            this.lrCheckPiertask = {};
            this.checkFile = [];
            this.lrCheckPierfoundation = []; // 基桩检测表格信息集合
            getPierTaskByFid({
                    fId: id
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.lrCheckPiertask = res.data.lrCheckPiertask; // 任务主表信息
                        this.checkFile = res.data.checkFile;
                        this.lrCheckPierfoundation =
                            res.data.lrCheckPiertask.pierfoundationList; // 基桩检测表格信息集合
                    } else {
                        return this.$message.error("基桩检测表格信息  获取失败", res.msg);
                    }
                })
                .catch(() => {
                    console.log("基桩检测表格信息集合  获取失败");
                    return false;
                });
        },

        // 点击当前行
        getRowInfo(e) {
            // this.row = e;
            // this.fId = e.fId;
        },
        // 点击设备编码
        openDialog(row) {
            this.fId = row.fId;
            this.flagNum = 1;
            this.dis = true;
            this.editText(this.fId);
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val;
        },
        // 获取页面容器高度
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
            this.dialogHeights = h * 0.888 + "px";
        },
    },
};